/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.exam;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: javaseapp - Ex2
 * <p>Powered by webrx On 2021-08-07 11:32:40
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex2 {
    public static void main(String[] args) {
        String s = "java-ajavajavajavabc-mysqljavascript-aajava 16ajav";
        String d = "java";
        int num = 0;
        if (s.contains(d)) {
            int index = s.indexOf(d);
            while (index != -1) {
                ++num;
                index = s.indexOf(d, index + d.length());
            }
        }
        System.out.printf("%s 在 %s 中出现了 %d 次。%n", d, s, num);

        //正则表达式
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher(s);
        int nn = 0;
        while (m.find()) {
            ++nn;
            //System.out.printf("%s ", m.group());
        }
        System.out.printf("%s 在 %s 中出现了 %d 次。%n", d, s, nn);

    }
}
